//路由公共配置

var func = require('{pub}/function');
var datagrid = require('datagrid');
var calendar = require('{plus}/calendar');
var util = require('util');
var func = require('{pub}/function');
var data = require('{pub}/data');
var template = require('template');
var component = require('component');
var layer = require('{pub}/layer');

Nui.doc.click(function(){
    $('.layer-position').hide();
})

return {
    template:require('../views/content'),
    data:{
        //导出url
        exportsUrl:'',
        //打印url
        printUrl:''
    },
    events:{
        'click .j-exports':'enabled exports',
        'click .j-print':'enabled print',
        'click .j-date':'showDate',
        'click .j-more':'more'
    },
    grid:{
        skin:'simple',
        paging:{
            pCount:20
        },
        dataField:'list'
    },
    //面包屑导航
    getNav:function(elem){
        this.data.nav.unshift(elem.text());
        var titleElem = elem.closest('.menu-child').siblings('.menu-title');
        if(titleElem.length){
            this.getNav(titleElem.children('span'))
        }
    },
    //读取用户与帐套数据
    initData:function(){
        this.data.account = data.get('account');
        this.data.user = data.get('user');
        this.data.date = this.data.account.date;
    },
    onChange:function(){
        this.data.nav = [];
        this.getNav($(this.target));
        this.initData();
    },
    //路由初始化或者弹出层初始化，self存在表示为弹出层
    onInit:function(self){
        var that = this;
        if(self){
            this.layer = self;
        }
        that.initCom();
        that.verify();
    },
    //初始化搜索栏复选框
    initCheckradio:function(){
        var that = this;
        that.header.find(':checkbox').checkradio({
            callback:function(){
                that.query();
            }
        });
    },
    //初始化搜索栏下拉框
    initSelect:function(){
        var that = this;
        that.header.find('select').imitSelect({
            callback:function(me, item){
                item && that.query();
            }
        });
    },
    //渲染后初始化组件
    initCom:function(){
        var that = this;
        that.header = that.element.find('.m-main-header');
        that.initCheckradio();
        that.initSelect();

        that.grid.container = that.element.find('.m-main-body');
        var onRender = that.grid.onRender;
        that.grid.onRender = function(self){
            if(typeof onRender === 'function'){
                onRender.call(that, self)
            }
            that.verify();
        }
        that.datagrid = datagrid(that.grid);
        that.datagrid.router = that;
        that.paging = that.datagrid.paging;

        component.init(this.element);
    },
    //渲染模版
    render:function(data){
        var tpl = this.template;
        this.data = $.extend(this.data, data||{});
        component.destroy(this.element);
        this.element.find('.l-body').html(template.render.call(tpl, tpl.body, this.data));
        this.initCom();
    },
    //判断按钮是否可用
    enabled:func.enabled,
    //处理查询数据
    filterQuery:function(data){
        this.data.date = data.date;
        data.date = data.date.replace(/(\d+)年(\d+)月/g, '$1-$2');
        return data
    },
    //获取查询数据
    getQuery:function(){
        var formElement = this.element.find('.m-main-header [name]');
        var data = this.filterQuery(util.getData(formElement).result);
        return data
    },
    //列表查询
    query:function(){
        $.extend(this.paging.condition, this.getQuery()||{});
        this.paging.query(true);
    },
    //显示期间日历
    showDate:function(e, elem){
        var that = this;
        var istwo = that.data.istwo;
        $.calendar({
            format:'yyyy年MM月',
            ismonth:true,
            istwo:istwo,
            iscope:istwo,
            monthcount:4,
            button:[{
                name:'confirm',
                text:'确定'
            }, {
                name:'close',
                text:'关闭'
            }],
            onchoose:function(date){
                if(istwo){
                    elem.val(date[0] + ' - ' + (date[1]||date[0]))
                }
                that.query()
            }
        })
    },
    setTitle:function(title){
        this.header.find('.title').html(title);
    },
    //导出pdf
    exports:function(e, elem){
        func.request(this.data.exportsUrl, this.getQuery());
    },
    //打印
    print:function(e, elem){
        func.print(this.data.printUrl, this.getQuery());
    },
    //按钮权限控制
    verify:function(){
        func.verify(this.data.account.authoritys)
    },
    moreLayer:function(){
        return {}
    },
    //更多条件
    more:function(e, elem){
        var that = this;
        if(!elem[0].layer){
            var tpl = template.render.call(that.template, that.template.moreDetail, that.data);
            var button = [{
                id:'cancel',
                text:'重置',
                style:{
                    float:'none'
                },
                callback:function(self, e){
                    self.reset();
                    e.stopPropagation();
                    return false;
                }
            }, {
                id:'confirm',
                text:'确定',
                name:'normal',
                style:{
                    float:'none'
                },
                callback:function(self, e){
                    that.query();
                    self.element.hide();
                    e.stopPropagation();
                }
            }]
            layer.position(e, tpl, elem, 'auto', button, null, 350, $.extend(true, {
                isHide:false,
                close:{
                    enable:false
                },
                position:{
                    left:0,
                    top:2
                }
            }, that.moreLayer()||{}))
        }
        else{
            elem[0].layer.element.show();
        }
        $('.ui-calendar').hide();
        e.stopPropagation();
    }
}